from django.contrib.syndication.feeds import Feed, FeedDoesNotExist
from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404
from django.utils.html import strip_tags
from pyplog.artists.models import Artist as Author
from pyplog.blog.models import Category, Post
from pyplog.config import config

class BlogPosts(Feed):
	title = config.title
	description =  "All blog posts"
	
	def items(self):
		return Post.public_objects.all()[:config.feed_items]

	def link(self):
		return reverse("blog-index")
	
class BlogPostsByGenericKey(Feed):
	title = config.title
	
	def get_object(self, bits):
		if len(bits) == 1:
			try:
				return self.key_class.objects.get(slug = bits[0])
			except self.key_class.DoesNotExist:
				pass
		raise FeedDoesNotExist
	
	def description(self, obj = None):
		return strip_tags(obj.postset_description)
	
	def items(self, obj = None):
		return obj.posts.all()[:config.feed_items]
	
	def link(self, obj):
		return obj.get_absolute_url()

class BlogPostsByCategory(BlogPostsByGenericKey):
	key_class = Category

class BlogPostsByAuthor(BlogPostsByGenericKey):
	key_class = Author
